﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Verifications;

namespace Pixysoft.Framework.Logs
{
    public class LoggerSetting : ILoggerSetting
    {
        bool isRunning = false;//判断配置是否已经生效 如果生效 则继续配置发生异常

        private long logCapacity = 1024 * 1024 * 1;

        private string logPath = StringHelper.default_log_filepath;

        private LogLevel logLevel = LogLevel.ALL;

        bool printToConsole = false;

        string filename = StringHelper.default_log_prefix;

        string modulename = "";//默认值是"" 不允许null


        public string LogPath
        {
            get { return logPath; }
            set
            {
                if (this.isRunning)
                    throw Exceptions.ManualVerificationFailedException("setting is using now, cannot change it.");

                if (string.IsNullOrEmpty(logPath))
                    throw Exceptions.VerificationFailedException(value);

                logPath = StringFilter.RegularPath(value);
            }
        }

        public string Filename
        {
            get { return filename; }
            set
            {
                if (this.isRunning)
                    throw Exceptions.ManualVerificationFailedException("setting is using now, cannot change it.");

                if (string.IsNullOrEmpty(filename))
                    throw Exceptions.VerificationFailedException(value);

                filename = StringFilter.GetFileName(value);
            }
        }

        public string Modulename
        {
            get { return modulename; }
            set
            {
                if (this.isRunning)
                    throw Exceptions.ManualVerificationFailedException("setting is using now, cannot change it.");

                if (string.IsNullOrEmpty(value))
                    modulename = "";
                else
                    modulename = value.Trim();
            }
        }



        public long LogCapacity
        {
            get { return logCapacity; }
            set { logCapacity = value; }
        }

        public LogLevel LogLevel
        {
            get { return logLevel; }
            set { logLevel = value; }
        }

        public bool PrintToConsole
        {
            get { return printToConsole; }
            set { printToConsole = value; }
        }


        public string GetLogFilePath()
        {
            return StringFilter.RegularPath(this.LogPath + this.Filename).Trim().ToUpper();
        }

        public string GetLogFilename()
        {
            return this.GetLogFilePath() + this.Filename.Trim().ToUpper() + StringHelper.default_log_postfix;
        }


        public void SetRunning()
        {
            this.isRunning = true;
        }

        /// <summary>
        /// 初始化加载的时候使用，其余的时候由代码保证
        /// </summary>
        public virtual void Verify()
        {
            if (string.IsNullOrEmpty(filename))
                filename = StringHelper.default_log_prefix;

            filename = StringFilter.GetFileName(filename);

            if (string.IsNullOrEmpty(logPath))
                logPath = StringHelper.default_log_filepath;

            logPath = StringFilter.RegularPath(logPath);

            if (LogCapacity < StringHelper.default_log_capacity)
                LogCapacity = StringHelper.default_log_capacity;
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder();

            builder.AppendFormat("path:{0},file:{1},module:{2},level:{3}",
                this.LogPath, this.filename, this.modulename, this.LogLevel);

            return builder.ToString();
        }
    }
}
